package cn.edu.scnu.Service.impl;

import cn.edu.scnu.Mapper.AddressMapper;
import cn.edu.scnu.Service.AddressService;
import cn.edu.scnu.entity.Address;
import cn.edu.scnu.entity.Return;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 根据用户账号查找全部收货地址
     * @param username
     * @return
     */
    @Override
    public Return findByUsername(String username) {
        LambdaQueryWrapper<Address> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Address::getUsername, username);
        return new Return().sussess(addressMapper.selectList(lqw));
    }

    /**
     * 新增收货地址
     * @param username
     * @param address
     * @return
     */
    @Override
    public Return addAddress(String username, Address address) {
        //为收货地址新增id
        address.setAddressId(UUID.randomUUID().toString().replaceAll("-",""));
        //为收货地址指定用户
        address.setUsername(username);
        boolean flag = addressMapper.insert(address)>0;
        if(flag){
            LambdaQueryWrapper<Address> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Address::getUsername, username);
            return new Return().sussess(addressMapper.selectList(lqw));
        }else{
            return new Return().error("服务器错误!_!");
        }
    }

    @Override
    public Return updateAddressById(Address address) {
        boolean flag = addressMapper.updateById(address)>0;
        if(flag){
            return findByUsername(address.getUsername());
        }else{
            return new Return().error("服务器错误!_!");
        }
    }

    @Override
    public Return setDefaultById(String addressId) {
        Address address = addressMapper.selectById(addressId);
        LambdaQueryWrapper<Address> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Address::isItDefault, true);
        lqw.eq(Address::getUsername, address.getUsername());
        Address oldAddress = addressMapper.selectOne(lqw);
        if(oldAddress!=null){
            oldAddress.setItDefault(false);
            addressMapper.updateById(oldAddress);
        }
        address.setItDefault(true);
        boolean flag = addressMapper.updateById(address) > 0;
        if(flag){
            return findByUsername(address.getUsername());
        }else{
            return new Return().error("服务器错误");
        }
    }

    @Override
    public Return deleteAddressById(String addressId) {
        Address address = addressMapper.selectById(addressId);
        boolean flag = addressMapper.deleteById(address) > 0;
        if(flag){
            return findByUsername(address.getUsername());
        }else{
            return new Return().error("服务器错误");
        }
    }

    @Override
    public Return findDefaultAddress(String username) {
        LambdaQueryWrapper<Address> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Address::getUsername, username);
        lqw.eq(Address::isItDefault, true);
        Address address = addressMapper.selectOne(lqw);
        return new Return().sussess(address);
    }
}
